-
Notifications
You must be signed in to change notification settings - Fork 184
Add information about how the clipboard/dataobject changes in .NET 10 #2129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
…mation and the desire to upgrade clipboard code
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A number of the samples aren't right. I've commented on some of them. They should be validated. I didn't go through all of them exhaustively, I'll look again after you've updated again and validated that your samples work.
.../winforms/migration/snippets/clipboard-dataobject-net10/net/csharp/BinaryFormatterSupport.cs
Show resolved
Hide resolved
.../winforms/migration/snippets/clipboard-dataobject-net10/net/csharp/BinaryFormatterSupport.cs
Outdated
Show resolved
Hide resolved
...informs/migration/snippets/clipboard-dataobject-net10/net/csharp/ITypedDataObjectExamples.cs
Outdated
Show resolved
Hide resolved
...op-guide/winforms/migration/snippets/clipboard-dataobject-net10/net/csharp/ModernApproach.cs
Show resolved
Hide resolved
|
||
public static void BrokenCustomTypeExample() | ||
{ | ||
// This worked in .NET 8 and earlier but silently fails starting with .NET 9 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should see a first chance exception under the debugger. The failure happens when OLE tries to make a copy of the data because of the default is copy = true
on SetData
. If it is set to false
it will just work in process as we don't actually have to serialize the type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JeremyKuhne If what is set to false
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
copy
when you manually put data into a DataObject
first. https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.clipboard.setdataobject?view=windowsdesktop-9.0#system-windows-forms-clipboard-setdataobject(system-object-system-boolean)
We probably should have created an overload so you didn't have to manual steps. Here is what SetData
does:
So, to avoid the copy
you have to turn SetData(format, data)
into SetDataObject(new DataObject(format, data), copy: false)
. Then if things aren't consumed external to the process it won't need to serialize. If copy
is true, the serialization is preemptive to allow the data to persist if you close the application.
.../winforms/migration/snippets/clipboard-dataobject-net10/net/csharp/PrimitiveTypesExamples.cs
Outdated
Show resolved
Hide resolved
.../winforms/migration/snippets/clipboard-dataobject-net10/net/csharp/PrimitiveTypesExamples.cs
Outdated
Show resolved
Hide resolved
|
||
`BinaryFormatter` was removed from the runtime in .NET 9 because of security vulnerabilities. This change broke clipboard and drag-and-drop operations with custom objects. .NET 10 introduces new APIs that use JSON serialization and type-safe methods to restore this functionality, improve security, and provide better error handling and cross-process compatibility. | ||
|
||
One significant change is that <xref:System.Windows.Clipboard.SetData(System.String,System.Object)?displayProperty=nameWithType> no longer works with custom types. It silently fails without storing data on the clipboard. <xref:System.Windows.Forms.Clipboard.GetData(System.String)?displayProperty=nameWithType> is obsolete in .NET 10 and shouldn't be used, even for built-in types. Use the new <xref:System.Windows.Forms.Clipboard.TryGetData*?displayProperty=nameWithType> and <xref:System.Windows.Forms.Clipboard.SetDataAsJson``1(System.String,``0)?displayProperty=nameWithType> methods for type-safe operations and JSON serialization of custom objects. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No longer works "by default".
To see the failure, you have to catch first chance exceptions under the debugger to see it fail on OLE trying to retrieve a copy of the data (which happens when you set if copy == true
, the default, otherwise on get).
:::code language="json" source="./snippets/how-to-enable-binaryformatter-clipboard-support/csharp/runtimeconfig.json"::: | ||
|
||
> [!IMPORTANT] | ||
> Without this specific runtime switch, clipboard operations won't fall back to `BinaryFormatter` even if general serialization support is enabled. This switch is required specifically for Windows Forms clipboard functionality. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The same procedures and switch are used for WPF. If you happen to have both WPF and WinForms in the same project, both Clipboard
classes will be enabled.
Summary
Fixes #2113
Internal previews
Internal previews
Toggle expand/collapse
Note
This table shows preview links for the 30 files with the most changes. For preview links for other files in this PR, select OpenPublishing.Build Details within checks.